import { reqgetcate, reqgetgoods } from "../../http/api";
import qs from "qs";

let oldid = null;
//初始值
const initState = {
  firstList: [],
  secondList: [],
  key: "",
  fid: 1,
};

//types
const TYPES = {
  CATE_CHANGE_FIRSTLIST: "CATE_CHANGE_FIRSTLIST",
  CATE_CHANGE_SECONDLIST: "CATE_CHANGE_SECONDLIST",
  CATE_CHANGE_KEY: "CATE_CHANGE_KEY",
  CATE_CHANGE_FID: "CATE_CHANGE_FID",
};

//修改state的reducer
const reducer = (state = initState, action) => {
  switch (action.type) {
    case TYPES.CATE_CHANGE_FIRSTLIST:
      return {
        ...state,
        firstList: action.firstList,
      };
    case TYPES.CATE_CHANGE_SECONDLIST:
      return {
        ...state,
        secondList: action.secondList,
      };
    case TYPES.CATE_CHANGE_KEY:
      return {
        ...state,
        key: action.key,
      };
    case TYPES.CATE_CHANGE_FID:
      return {
        ...state,
        fid: action.fid,
      };
    default:
      return state;
  }
};

//actionCreators
export const actions = {
  changeFirstList: (firstList) => ({
    type: TYPES.CATE_CHANGE_FIRSTLIST,
    firstList,
  }),
  //请求一级分类列表
  reqFirstList: () => {
    return (dispatch, getState) => {
      let {
        cate: { firstList },
      } = getState();
      if (firstList.length > 0) {
        return;
      }
      reqgetcate().then((res) => {
        if (res.data.code == 200) {
          dispatch(actions.changeFirstList(res.data.list));
        }
      });
    };
  },

  changeSecondList: (secondList) => ({
    type: TYPES.CATE_CHANGE_SECONDLIST,
    secondList,
  }),
  //请求一级分类下的商品
  reqSecondList: (fid = 1) => {
    return (dispatch, getState) => {
      if (oldid == fid) {
        return;
      }
      oldid = fid;
      dispatch(actions.changeFid(fid));
      reqgetgoods({ fid }).then((res) => {
        if (res.data.code == 200) {
          dispatch(actions.changeSecondList(res.data.list));
        }
      });
    };
  },

  changeKey: (key) => ({
    type: TYPES.CATE_CHANGE_KEY,
    key,
  }),
  changeFid: (fid) => ({
    type: TYPES.CATE_CHANGE_FID,
    fid,
  }),
  //一进页面判断是否有fid
  isFid: (props) => {
    return (dispatch, getState) => {
      if (props.location.search) {
        let search = props.location.search.slice(1);
        let fid = qs.parse(search).fid;
        dispatch(actions.changeFid(fid));
        dispatch(actions.reqSecondList(fid));
      } else {
        dispatch(actions.reqSecondList());
      }
    };
  },
  //切换标签
  change: (e) => {
    return (dispatch, getState) => {
      let {
        cate: { firstList },
      } = getState();
      dispatch(actions.changeFid(firstList[e - 1].id));
      dispatch(actions.reqSecondList(firstList[e - 1].id));
    };
  },
  //获取搜索框的值
  getValue: (e) => {
    return (dispatch, getState) => {
      dispatch(actions.changeKey(e.target.value));
    };
  },
  //点击搜索按钮
  search: (push) => {
    return (dispatch, getState) => {
      let {
        cate: { key },
      } = getState();
      push("/search?key=" + key);
    };
  },
};

//导出数据
export const getFirstList = (state) => state.cate.firstList;
export const getSecondList = (state) => state.cate.secondList;
export const getKey = (state) => state.cate.key;
export const getFid = (state) => state.cate.fid;

//导出reducer
export default reducer;
